<?php

namespace App\Models\Factory;

use App\Models\AbsModelFactory;
use App\Models\Orm\DataProductCount;
use App\Models\Orm\DataUserCount;
use Illuminate\Support\Facades\DB;

/**
 * 产品数据统计工厂方法
 *
 * Class DataProductCountFactory
 * @package App\Models\Factory
 */
class DataProductCountFactory extends AbsModelFactory
{
    /**
     * 获取单日产品总览
     * @return array
     */
    public static function getDayDataProductCount($date)
    {
        $res = DataProductCount::select([
            'data_product_count.product_id',
            'data_product_count.product_name',
            'data_product_count.details_total',
            'data_product_count.details_user_total',
            'data_product_count.apply_total',
            'data_product_count.user_total',
            'product.product_logo',
        ])->leftjoin('product', 'product.id', '=', 'data_product_count.product_id')
            ->where('data_product_count.create_date', $date)
            ->get();

        return $res ?: [];
    }

    /**
     * 获取30天产品总览
     * @return array
     */
    public static function getMonthDataProductCount()
    {
        $start = date('Y-m-d', strtotime('-31 day'));
        $end = date('Y-m-d', strtotime('-1 day'));

        $res = DataProductCount::select([
            DB::raw('sum(data_product_count.details_total) as details_total'),
            DB::raw('sum(data_product_count.details_user_total) as details_user_total'),
            DB::raw('sum(data_product_count.apply_total) as apply_total'),
            DB::raw('sum(data_product_count.user_total) as user_total'),
            'product.id',
            'product.name',
            'product.product_logo',
        ])->leftjoin('product', 'product.id', '=', 'data_product_count.product_id')
            ->where('create_date', '>', $start)
            ->where('create_date', '<=', $end)
            ->groupby('data_product_count.product_id')
            ->get();

        return $res ?: [];
    }

    /**
     * 获取某产品统计列表
     *
     * @param $product_id
     * @return mixed
     */
    public static function getProductCountList($product_id)
    {
        $res = DataProductCount::select('*')
            ->where('product_id', $product_id)
            ->get();

        return $res;
    }

}
